<?php

class User_IndexController extends Veneer_Controller_Module
{
    protected $_dbArray =array('User');
    protected $_moduleName = 'user';

    function indexAction()
    {
        $this->view->title = "User List";

        $entry = new User();
        $this->view->entries = $entry->fetchAll();
		// listHelper
    }

    function addAction()
    {
		static  $formArray = array();

        $this->view->title = "Add New Album";

		$this->addHelper($formArray);

    }

function editAction()
{
    $this->view->title = "Edit Album";
    $album = new Album();

    if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {


    	$id = (int)$this->_request->getPost('id');
    	$artist = $filter->filter($this->_request->getPost('artist'));
    	$artist = trim($artist);
    	$title = trim($filter->filter($this->_request->getPost('title')));

        if ($id !== false) {
            if ($artist != '' && $title != '') {
				$data = array(
				    'artist' => $artist,
				    'title'  => $title,
				);
				$where = 'id = ' . $id;
				$album->update($data, $where);

	    	    $this->_redirect('/');
	    	    return;
            } else {
                $this->view->album = $album->fetchRow('id='.$id);
	        }
        }
    } else {
        // album id should be $params['id']
        $id = (int)$this->_request->getParam('id', 0);
	    if ($id > 0) {
	        $this->view->album = $album->fetchRow('id='.$id);
	    }
    }

    // additional view fields required by form
    $this->view->action = 'edit';
    $this->view->buttonText = 'Update';

    $this->render();
}

    function loginAction()
    {
        $this->view->title = "Login";
       if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
			$userObj = new User();
            $filter = new Zend_Filter_Alpha();

            $username = $filter->filter($this->_request->getPost('username'));
            $password = $filter->filter($this->_request->getPost('password'));
	        $user_rec = $userObj->fetchRow("username='".$username."'");

			print "password=" . $user_rec->password;
			 if (empty($username)) {
                $this->view->message = 'Please provide a username.';
            } else {
                // setup Zend_Auth adapter for a database table
              //  Zend_Loader::loadClass('Zend_Auth_Adapter_DbTable');
              $dbAdapter = Zend_Registry::get('dbAdapter');
                $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
                $authAdapter->setTableName('user');
                $authAdapter->setIdentityColumn('username');
                $authAdapter->setCredentialColumn('password');

                // Set the input credential values to authenticate against
                $authAdapter->setIdentity($username);
                $authAdapter->setCredential($password);

                // do the authentication
                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($authAdapter);
                if ($result->isValid()) {
                    // success : store database row to auth's storage system
                    // (not the password though!)
                    $data = $authAdapter->getResultRowObject(null, 'password');
                    $auth->getStorage()->write($data);
                    $this->_redirect('/');
                } else {
                    // failure: clear database row from session
                    $this->view->message = 'Login failed.';
                }
            }

 	    	// $this->_redirect('/');
		}

        // redirect back to the album list unless we have rendered the view

	}
  

}